package com.banciyuan.bcywebview.base.view.textview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.utils.common.UIUtils;

/**
 * Created by squall on 2017/5/19.
 */

public class FocusButton extends AppCompatTextView {

    private String onSelectedText;
    private String onUnselectedText;
    private int boardColor;
    private int selectBoardColor;
    private int onSelectColor;
    private int onUnselectColor;
    private boolean useDrawable;
    private boolean isCheck;

    private Paint textSelectedPaint;
    private Paint textUnselectedPaint;
    private Paint imageDrawable;
    private Paint selectedbackPaint;
    private Paint unselectedbackPaint;

    private Path path = new Path();

    private RectF rect = new RectF();

    private int padding_top;
    private int padding_left;
    private int padding_drawable;
    private int radius;
    private int single_dp;

    private int height;
    private int width;


    public FocusButton(Context context) {
        super(context);
        init();
    }

    public FocusButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        getAttr(context, attrs);
        init();
    }

    public FocusButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        getAttr(context, attrs);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(width, height);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (isCheck || isSelected()) {
            canvas.drawRoundRect(rect, radius, radius, selectedbackPaint);
            padding_left = (int) ((width - getTextWidth(onSelectedText, textSelectedPaint)) / 2);
            canvas.drawText(onSelectedText, padding_left, getBaseLine(textSelectedPaint), textSelectedPaint);
        } else {
            float imgWidth = 0;
            if (useDrawable) {
                imgWidth = getImageWidth() + padding_drawable;
                padding_left = (int) ((width - getTextWidth(onUnselectedText, textUnselectedPaint) - imgWidth) / 2);
                padding_top = (int) ((height - getTextHeight(textUnselectedPaint) / 2));
                path.moveTo(padding_left + getImageWidth() / 2, UIUtils.dip2px(9, getContext()) + 1);
                path.lineTo(padding_left + getImageWidth() / 2, UIUtils.dip2px(19, getContext()) + 1);
                path.moveTo(padding_left, height / 2);
                path.lineTo(padding_left + getImageWidth(), height / 2);
                canvas.drawPath(path, imageDrawable);
            } else {
                padding_left = (int) ((width - getTextWidth(onUnselectedText, textUnselectedPaint)) / 2);
            }
            canvas.drawRoundRect(rect, radius, radius, unselectedbackPaint);
            canvas.drawText(onUnselectedText, padding_left + imgWidth, getBaseLine(textUnselectedPaint), textUnselectedPaint);
        }
    }

    private void init() {
        textSelectedPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textSelectedPaint.setColor(onSelectColor);
        textSelectedPaint.setTextSize(getTextSize());

        textUnselectedPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textUnselectedPaint.setColor(onUnselectColor);
        textUnselectedPaint.setTextSize(getTextSize());

        unselectedbackPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        unselectedbackPaint.setStrokeWidth(UIUtils.dip2px(1, getContext()));
        unselectedbackPaint.setColor(boardColor);
        unselectedbackPaint.setStyle(Paint.Style.STROKE);

        selectedbackPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        selectedbackPaint.setColor(selectBoardColor);
        selectedbackPaint.setStyle(Paint.Style.FILL);

        imageDrawable = new Paint(Paint.ANTI_ALIAS_FLAG);
        imageDrawable.setColor(onUnselectColor);
        imageDrawable.setStrokeWidth(UIUtils.dip2px(1, getContext()));
        imageDrawable.setStyle(Paint.Style.STROKE);

        height = UIUtils.dip2px(29, getContext());
        width = UIUtils.dip2px(69, getContext());
        single_dp = UIUtils.dip2px(1, getContext());

        rect.top = single_dp;
        rect.left = single_dp;
        rect.right = width - single_dp;
        rect.bottom = height - single_dp;

        padding_drawable = UIUtils.dip2px(4, getContext());
        radius = UIUtils.dip2px(3, getContext());

    }

    private void getAttr(Context mContext, AttributeSet attrs) {
        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.FocusButton);
        useDrawable = a.getBoolean(R.styleable.FocusButton_useDrawable, false);
        onSelectColor = a.getColor(R.styleable.FocusButton_selectedColor, getResources().getColor(R.color.down_warn));
        onUnselectColor = a.getColor(R.styleable.FocusButton_unselectedColor, getResources().getColor(R.color.pink));
        onSelectedText = a.getString(R.styleable.FocusButton_selectText);
        onUnselectedText = a.getString(R.styleable.FocusButton_unselectText);
        boardColor = a.getColor(R.styleable.FocusButton_boardColor, getResources().getColor(R.color.pink));
        selectBoardColor = a.getColor(R.styleable.FocusButton_selecedBoardColor, getResources().getColor(R.color.grey_one_level));
        isCheck = a.getBoolean(R.styleable.FocusButton_isCheck, false);
        if (onSelectedText == null) {
            onSelectedText = "";
        }
        if (onUnselectedText == null) {
            onUnselectedText = "";
        }
    }

    private float getTextWidth(String word, Paint textPaint) {
        if (!TextUtils.isEmpty(word)) {
            return textPaint.measureText(word);
        } else {
            return 0f;
        }
    }

    private float getImageWidth() {
        return UIUtils.dip2px(10, getContext());
    }

    private float getTextHeight(Paint textPaint) {
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        return fontMetrics.bottom - fontMetrics.top;
    }

    private float getBaseLine(Paint textPaint) {
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        return (height - fontMetrics.top - fontMetrics.bottom) / 2;
    }

    public void setCheck(boolean check) {
        isCheck = check;
        invalidate();
    }
}
